<script src="../../resources/js-test.js"></script>
<div id="console"></div>
<script>
function testPartialSelection(initialContents, expectedContents, command, argument) {
    testContainer.innerHTML = initialContents;
    var selection = window.getSelection();
    selection.collapse(testContainer, 0);
    selection.modify("move", "forward", "character");
    selection.extend(selection.focusNode, 3)
    document.execCommand(command, false, argument);
    if (testContainer.innerHTML === expectedContents) {
        testPassed(command + " command converted " + initialContents + " to " + expectedContents);
    } else {
        testFailed(command + " command converted " + initialContents + " to " + testContainer.innerHTML + ", expected " + expectedContents);
    }
}

var testContainer = document.createElement("div");
testContainer.contentEditable = true;
document.body.appendChild(testContainer);

testPartialSelection('<a id="abc"><b>fbabaz</b></a>', '<b><a id="abc">f</a><a href="http://www.google.com">ba</a><a>baz</a></b>', 'createLink', 'http://www.google.com');
testPartialSelection('<a name="abc"><b>fbabaz</b></a>', '<b><a name="abc">f</a><a href="http://www.google.com">ba</a><a>baz</a></b>', 'createLink', 'http://www.google.com');
testPartialSelection('<a id="abc" name="abc"><b>fbabaz</b></a>', '<b><a id="abc" name="abc">f</a><a href="http://www.google.com">ba</a><a>baz</a></b>', 'createLink', 'http://www.google.com');

testPartialSelection('<a id="abc"><b>fbabaz</b></a>', '<b><a id="abc">f</a>ba<a>baz</a></b>', 'unlink');
testPartialSelection('<a name="abc"><b>fbabaz</b></a>', '<b><a name="abc">f</a>ba<a>baz</a></b>', 'unlink');
testPartialSelection('<a id="abc" name="abc"><b>fbabaz</b></a>', '<b><a id="abc" name="abc">f</a>ba<a>baz</a></b>', 'unlink');

testPartialSelection('<span id="abc" name="abc"><b>fbabaz</b></span>', '<span id="abc" name="abc"><b>f<a href="http://www.google.com">ba</a>baz</b></span>', 'createLink', 'http://www.google.com');

document.body.removeChild(testContainer);
var successfullyParsed = true;
</script>
